package openBeeTester;

import java.io.IOException;
import java.util.Scanner;

import openbee.Communication;
import openbee.IModuleControl;
import openbee.ModuleControl;
import openbee.ResponseEvent;
import openbee.ResponseListener;
import openbee.enums.Register;

public class tester implements ResponseListener {

	static Scanner choose = new Scanner(System.in);
	static Communication comm;
	static String command = null;
	static ModuleControl modCtrl;

	public static void register(IModuleControl callback) {
        callback.DisplayProductInfo();
    }
	
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub

		comm = new Communication();
		comm.addMoodListener(new tester());

		Thread t = new Thread() {
			public void run() {
				try {
					// comm.Connect("10.1.1.10", 3008, "root", "admin");
					comm.Connect("89.68.23.43", 8081, "root", "maczek31");
					modCtrl = new ModuleControl(comm);
					comm.OpenChannel("10.1.1.10", "1234");

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		};
		t.start();

		// ====== Java 6

		int j = 0;
		while (!"end".equals(command)) {
			command = choose.nextLine();
			if (command.equals("g")) {
				comm.CloseChannel();
				comm.Disconnect();
			} else if (command.equals("I")) {
				modCtrl.DisplayProductInfo();
			} else if (command.equals("S")) {
				modCtrl.ScanNetwork((byte)0);
			} else if (command.equals("R")) {
				modCtrl.ReadRegisterLocally(Register.TimerCounter0);
			} else if (command.equals("M")) {

				if (modCtrl.getCoordinator().getNodes().size() > 0) {
					modCtrl.ReadRegisterRemotely(modCtrl.getCoordinator()
							.getNodes().get(0).getEui64(),
							Register.TimerCounter0);
				}

			} else if (command.equals("W")) {
				modCtrl.WriteRegisterLocally(Register.TransmitPowerLevel, 3);
			} else if (command.equals("P")) {

				if (modCtrl.getCoordinator().getNodes().size() > 0) {
					/*modCtrl.WriteRegisterRemotely(modCtrl.getCoordinator()
							.getNodes().get(0).getEui64(), Register.TimerCounter0, 0x0004, "password");*/
				}
			} else
				comm.Send(command);
		}

	}

	@Override
	public void moodReceived(ResponseEvent event) {
		// TODO Auto-generated method stub
		System.out.println("RX: " + event.get_commandArguemnts().get_prompt());
	}
}
